package com.t.bricks.bussiness.db.dao.roleandright.impl;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.t.bricks.bussiness.db.dao.roleandright.RoleAndRightDao;
import com.t.bricks.bussiness.db.entity.roleandright.RoleAndRightSimpl;
import com.t.bricks.bussiness.db.mapper.roleandright.RoleAndRightMapper;
import com.t.bricks.model.Log;
import com.t.bricks.model.MsgEmity;

@Repository
public class RoleAndRightDaoImpl implements RoleAndRightDao {
	
	@Autowired
	private RoleAndRightMapper roleAndRightMapper;
	
	/**
	 * 添加权限信息
	 * @param sRoleId
	 * @param sRightList
	 * @param sCreator
	 * @return
	 */
	@Transactional
	@Override
	public MsgEmity add(String sRoleId, String sRightList, String sCreator) {
		String[] str = sRightList.replaceAll(" ", "").split(",");
		List<String> list = Arrays.asList(str);// 权限编号集合转换

		int iCode = 7001;
		try {
			int j = roleAndRightMapper.findCountByRoleId(sRoleId);
			if (j > 0) {
				int k = roleAndRightMapper.del(sRoleId);
				if (k < 1) {
					iCode = 7002;
					Log.error("删除未能得到预期影响值(预期大于等于1):", k);
					throw new RuntimeException("清理旧数据失败！");
				}
			}
			
			if (null != list && list.size() > 0) {
				int i = roleAndRightMapper.add(sRoleId, list, sCreator);
				if (i < 1) {
					iCode = 7003;
					Log.error("保存未能得到预期影响值(预期大于等于1):", i);
					throw new RuntimeException("保存数据失败！");
				}
			}
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			Log.error("保存时发生异常:", e.getMessage());
			return new MsgEmity(false, "保存失败！", iCode);
		}

		return new MsgEmity(true, "保存成功！", 7999);
	}

	/**
	 * 删除权限信息
	 * @param sRoleId
	 * @return
	 */
	@Override
	public MsgEmity del(String sRoleId) {
		try {
			int i = roleAndRightMapper.findCountByRoleId(sRoleId);
			if (i > 0) {
				roleAndRightMapper.del(sRoleId);
			}
		} catch (Exception e) {
			Log.error("删除时发生异常:", e.getMessage());
			return new MsgEmity(false, "删除失败！", 7001);
		}
		
		try {
			int i = roleAndRightMapper.findCountByRoleId(sRoleId);
			if (i > 0) {
				 return new MsgEmity(false, "删除失败！", 7002);
			}
		} catch (Exception e) {
			Log.error("查询时发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败！", 7003);
		}
		
		return new MsgEmity(true, "删除成功！", 7999);
	}
	
	/**
	 * 根据角色sRoleId取权限信息
	 * @param sRoleId
	 * @return
	 */
	@Override
	public MsgEmity findByRoleId(String sRoleId) {
		List<RoleAndRightSimpl> list = null;
		try {
			list = roleAndRightMapper.findByRoleId(sRoleId);
			if (null == list) {
				return new MsgEmity(false, "数据不存在！", 7001);
			}
		} catch (Exception e) {
			Log.error("查询发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败！", 7002);
		}
		
		return new MsgEmity(true, "查询成功！", list);
	}

}
